package technion.gc.web;

import java.util.ArrayList;

import technion.gc.api.interfaces.AbstractGraphNode;
import technion.gc.api.interfaces.IGraph;
import technion.gc.api.interfaces.ITraversalStrategy;

public class WebTraversalStrategy implements ITraversalStrategy {
	private IGraph graph;
	private AbstractGraphNode nextNode;
	private ArrayList<AbstractGraphNode> openList = new ArrayList<AbstractGraphNode>();

	@Override
	public void setGraph(IGraph _graph) {
		graph = _graph;
		nextNode = graph.getRoot();
	}

	@Override
	public AbstractGraphNode getNextGraphNode() {
		return nextNode;
	}

	@Override
	public boolean hasNextGraphNode() {
		return nextNode == graph.getRoot() || !openList.isEmpty();
	}

	@Override
	public void encountered(AbstractGraphNode graphNode) {
		// BFS
		openList.remove(graphNode);
		if (nextNode.getNeighbors() == null) {
			return;
		}
		openList.addAll(nextNode.getNeighbors());
		nextNode = openList.get(0);
	}

	@Override
	public void encounteredAgain(AbstractGraphNode graphNode) {
		openList.remove(graphNode);
		if (!openList.isEmpty())
			nextNode = openList.get(0);
	}

	@Override
	public void cleanup() {
		openList = new ArrayList<AbstractGraphNode>();
	}

}
